<template>
  <div>
    <el-dialog
      :title="info.isadd?'添加':'修改'"
      :visible.sync="info.isshow"
      @opened="opened"
      @closed="closed"
    >
      {{form}}
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="onChange"
          >
            <img v-if="form.img" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">{{info.isadd?'添加':'修改'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { reqbanneradd, reqbannerdit, reqcatelist, } from "../../../request/api.js";
export default {
  components: {},
  data() {
    return {
      form: {
        title: "",
        img: "",
        status: 1
      },
      imageUrl: ""
    };
  },
  computed: {
    ...mapGetters({
      info: "banner/info",
      bannerinfo: "banner/bannerinfo",
    })
  },
  methods: {
    ...mapActions({
      reqbannerlist: "banner/bannerlist",
    }),
    cancel() {
      this.info.isshow = false;
    },
    add() {
      if (this.info.isadd) {
        reqbanneradd(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqbannerlist();
            this.info.isshow = false;
          }
        });
      } else {
        reqbannerdit(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqbannerlist();
            this.info.isshow = false;
          }
        });
      }
    },
    opened() {
      if (!this.info.isadd) {
        this.imageUrl = this.$pre + this.bannerinfo.img;
        this.form = this.bannerinfo;
      }
    },
    onChange(e) {
      let file = e.raw;
      this.form.img = file;
      this.imageUrl = URL.createObjectURL(file);

    },
    closed() {
      if (!this.info.isadd) {
        this.form = {
          bannername: "",
          attrs: "",
          status: 1
        };
      }
    }
    // addArr() {
    //   this.arr.push({ value: this.arr[0].value });
    // }
  },
  mounted() {
    this.reqbannerlist();
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>